<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ajax 请求测试</title>
</head>
<body>
    <script type="module">
        import {ajax,get,getjson,post} from './main.js';

        const url = 'https://www.imooc.com/api/http/search/suggest?words=js';

        const promise =  getjson(url,{
            params:{
                username:'swk',
                data:{age:5000}
            }
        });

        const {
            ERROR_HTTP_CODE,
            ERROR_REQUEST,
            ERROR_TIMEOUT,
            ERROR_ABORT,
        } = promise;

        // 成功
        promise.then(response =>{
            console.log(response);
        });

        // 失败
        promise.catch(err =>{
            // console.log(err);
            switch(err.type){
                case ERROR_HTTP_CODE:
                    console.log(err.text); break;
                case ERROR_ABORT:
                    console.log(err.text); break;
                case ERROR_REQUEST:
                    console.log(err.text); break;
                case ERROR_TIMEOUT:
                    console.log(err.text); break;
            }
        });


    </script>
</body>
</html>
